﻿/*********************************************************
 * 开发人员：小兵
 * 创建时间：2012-10-26 16:18:07
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
namespace Mozlite.Components.Modules
{
    using System;
    using System.Web;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// 页面查询。
    /// </summary>
    public class PageQuery : BaseCultureQuery
    {
        /// <summary>
        /// 初始化类型<see cref="PageQuery"/>。
        /// </summary>
        public PageQuery() {
            this.Fields = "PageID,RoleLevel,[Title],Summary,[Key],[ImageUrl],CategoryID,[Views],[DefaultUrlFormat]";
        }

        /// <summary>
        /// 附加条件。
        /// </summary>
        /// <returns>获取附加条件。</returns>
        protected override string AppendWheres()
        {
            string where = "CultureName = @CultureName AND RoleLevel <= @RoleLevel";
            if(CategoryID != null)
                where += " AND CategoryID = @CategoryID";
            return where;
        }

        /// <summary>
        /// 获取或设置分类ID。
        /// </summary>
        public int? CategoryID { get; set; }

        /// <summary>
        /// 获取参数匿名类型。
        /// </summary>
        /// <returns>返回参数匿名类型。</returns>
        protected override object AppendParameters()
        {
            if(CategoryID == null)
                return new { RoleLevel = Self.RoleLevel, CultureName = CultureName };
            return new { RoleLevel = Self.RoleLevel, CategoryID = CategoryID.Value, CultureName = CultureName };
        }

        /// <summary>
        /// 获取分页列表。
        /// </summary>
        /// <typeparam name="T">实例类型。</typeparam>
        /// <param name="size">返回总记录数。</param>
        /// <returns>返回实例列表。</returns>
        public override List<T> Load<T>(out int size)
        {
            return Fetch<T>("moz_Pages", out size);
        }

        /// <summary>
        /// 返回页面列表。
        /// </summary>
        /// <param name="size">返回总记录数。</param>
        /// <returns>返回页面列表。</returns>
        public List<Page> Load(out int size) {
            return Load<Page>(out size);
        }
    }
}
